﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace GetWindowMessageDemo
{
    
    [StructLayout(LayoutKind.Sequential)]
    public struct POINTAPI
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MSG
    {
        public IntPtr hwnd;
        public int message;
        public long wParam;
        public long lParam;
        public int time;
        public POINTAPI pt;
    }
    public enum MessageType
    {
        UPGRADE_MESSAGE = 0,
        WARNING_MESSAGE = 1,
        LINK_STATE_UP_MESSAGE = 2,
        LINK_STATE_DOWN_MESSAGE = 4,
        Seed_Test_BCM = 8
    }

    public partial class Form1 : Form
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int RegisterWindowMessage(string lpString);

        public int upgradeMessageId = 0;
        public int warningMessageId = 0;
        public int linkStateUpMessageId = 0;
        public int linkStateDownMessageId = 0;
        public int SeedTestBcmId = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            upgradeMessageId = RegisterWindowMessage(MessageType.UPGRADE_MESSAGE.ToString());
            warningMessageId = RegisterWindowMessage(MessageType.WARNING_MESSAGE.ToString());
            linkStateUpMessageId = RegisterWindowMessage(MessageType.LINK_STATE_UP_MESSAGE.ToString());
            linkStateDownMessageId = RegisterWindowMessage(MessageType.LINK_STATE_DOWN_MESSAGE.ToString());
            SeedTestBcmId = RegisterWindowMessage(MessageType.Seed_Test_BCM.ToString());
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == upgradeMessageId)
            {
                textBox1.Text += getPrintMsg(MessageType.UPGRADE_MESSAGE, m);
                return;
            }
            if (m.Msg == warningMessageId)
            {
                textBox1.Text += getPrintMsg(MessageType.WARNING_MESSAGE, m);
                return;
            }
            if (m.Msg == linkStateUpMessageId)
            {
                textBox1.Text += getPrintMsg(MessageType.LINK_STATE_UP_MESSAGE, m);
                return;
            }
            if (m.Msg == linkStateDownMessageId)
            {
                textBox1.Text += getPrintMsg(MessageType.LINK_STATE_DOWN_MESSAGE, m);
                return;
            }
            if (m.Msg == SeedTestBcmId)
            {
                textBox1.Text += getPrintMsg(MessageType.Seed_Test_BCM, m);
                return;
            }
            base.WndProc(ref m);
        }

        protected string getPrintMsg(MessageType messageType, Message m)
        {
            return messageType.ToString() + " " + m.WParam.ToString() + " " + m.LParam.ToString() + "\r\n";
        }
    }
}

